package com.right.health.app.ui.registered.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.magic.basic.utils.ToastUtil;
import com.magic.basic.widget.refresh.OnLastItemVisibleListener;
import com.magic.basic.widget.refresh.SwipeListRefreshLayout;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.base.BasePresenterFragment;
import com.right.health.app.bean.DateOffsetInfo;
import com.right.health.app.bean.DepartmentInfo;
import com.right.health.app.bean.DoctorResponse;
import com.right.health.app.bean.DoctorScheduleResponse;
import com.right.health.app.constants.BroadCastKey;
import com.right.health.app.dialog.BaseDialog;
import com.right.health.app.dialog.ListBottomSheetDialog;
import com.right.health.app.dialog.RegisteredDoctorDialog;
import com.right.health.app.net.DoctorApi;
import com.right.health.app.ui.registered.ChooseDepartmentActivity;
import com.right.health.app.ui.registered.RegisteredConfirmActivity;
import com.right.health.app.ui.registered.adapter.DoctorAdapter;
import com.right.health.app.ui.registered.adapter.DoctorBottomSheetAdapter;

import java.util.ArrayList;
import java.util.List;

/*
 * Created by hopeliao on 2019-09-26
 * 医生列表
 *
 */
public class DoctorFragment extends BasePresenterFragment implements View.OnClickListener, OnHttpListener, OnLastItemVisibleListener,
        SwipeRefreshLayout.OnRefreshListener {

    private static final String EXTRA_KEY = "extra:dept";

    private static final int HTTP_CODE_REFRESH = 1;
    private static final int HTTP_CODE_LOAD_MORE = 2;

    private DoctorAdapter mAdapter;

    private RegisteredDoctorDialog mTipsDialog;

    private ListBottomSheetDialog mDoctorBottomDialog;

    private DoctorBottomSheetAdapter mBottomSheetAdapter;

    private DateOffsetInfo mDateInfo;

    private DoctorApi mDoctorApi;

    private SwipeListRefreshLayout mRefreshView;

    private int mPageNo;

    private DepartmentInfo mDepartmentInfo;
    private DoctorResponse.DoctorInfo mChooseDoctorInfo;
    private DoctorScheduleResponse.DoctorScheduleInfo mChooseScheduleInfo;

    public List<DoctorResponse.DoctorInfo> mDataSource = new ArrayList<>();

    private String mSyscommunityCode;

    @Override
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle, String s) {
        return layoutInflater.inflate(R.layout.choose_doctor_fragment, viewGroup, false);
    }

    @Override
    public void findView() {
        mRefreshView = (SwipeListRefreshLayout) findViewById(R.id.refresh_view);
        mRefreshView.setLastItemVisibleListener(this);
        mRefreshView.setOnRefreshListener(this);

        ListView listView = (ListView) findViewById(R.id.listview);
        mAdapter = new DoctorAdapter(getContext());
        listView.setAdapter(mAdapter);
        mAdapter.setOnClickListener(this);
    }

    @Override
    public void initialize() {
        mDoctorApi = new DoctorApi();
        mDoctorApi.setListener(this);

        Bundle bundle = getArguments();
        if (bundle != null){
            if (bundle.containsKey(BroadCastKey.DATA)) {
                mDateInfo = bundle.getParcelable(BroadCastKey.DATA);
            }
            if(bundle.containsKey(EXTRA_KEY)) {
                mDepartmentInfo = bundle.getParcelable(EXTRA_KEY);
            }
            if(bundle.containsKey(ChooseDepartmentActivity.EXTRA_CODE)) {
                mSyscommunityCode = bundle.getString(ChooseDepartmentActivity.EXTRA_CODE);
            }
        }


        mRefreshView.setRefreshing(true);
        onRefresh();
    }

    public static DoctorFragment applyFragment(DepartmentInfo departmentInfo, DateOffsetInfo date, String syscommunitycode) {
        DoctorFragment doctorFragment = new DoctorFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable(BroadCastKey.DATA, date);
        bundle.putParcelable(EXTRA_KEY, departmentInfo);
        bundle.putString(ChooseDepartmentActivity.EXTRA_CODE, syscommunitycode);

        doctorFragment.setArguments(bundle);
        return doctorFragment;
    }

    @Override
    public void onClick(View v) {
        if (!isAdded()) {
            return;
        }
        if (v.getId() == R.id.btn_handle) {
            if (mDateInfo != null) {
                DoctorResponse.DoctorInfo info = (DoctorResponse.DoctorInfo) v.getTag();
                mDoctorApi.setTag(info);
                mDoctorApi.getDoctorSchedule(mSyscommunityCode, info.doctorid, info.doctorlevelcode, mDateInfo.startDate, mDateInfo.endDate);
            }
//            getChooseDialog().show();

//            if (mTipsDialog == null) {
//                mTipsDialog = new RegisteredDoctorDialog(getActivity());
//                mTipsDialog.setDelegate(new BaseDialog.IDialogDelegate() {
//                    @Override
//                    public void onDialogCallback(BaseDialog dialog) {
//                        startIntent(RegisteredConfirmActivity.class);
//                    }
//                });
//            }
//            mTipsDialog.show();
        }
    }

    private ListBottomSheetDialog getChooseDialog(List<DoctorScheduleResponse.DoctorScheduleInfo> data) {
        if (mDoctorBottomDialog == null) {
            mDoctorBottomDialog = new ListBottomSheetDialog(getActivity());
            mBottomSheetAdapter = new DoctorBottomSheetAdapter(getActivity());
            mDoctorBottomDialog.setAdapter(mBottomSheetAdapter);
            mDoctorBottomDialog.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    DoctorScheduleResponse.DoctorScheduleInfo info = (DoctorScheduleResponse.DoctorScheduleInfo) parent.getItemAtPosition(position);
                    if (info != null) {
                        mChooseScheduleInfo = info;

                        dismissBottomDialog();
                        getTipsDialog().show();
                    }
                }
            });
        }

        mBottomSheetAdapter.setDataSource(data);
        return mDoctorBottomDialog;
    }

    public RegisteredDoctorDialog getTipsDialog() {
        if (mTipsDialog == null) {
            mTipsDialog = new RegisteredDoctorDialog(getActivity());
            mTipsDialog.setDelegate(new BaseDialog.IDialogDelegate() {
                @Override
                public void onDialogCallback(BaseDialog dialog) {
                    if (!isAdded()) {
                        return;
                    }
                    dismissTipsDialog();
                    RegisteredConfirmActivity.applyActivity(getActivity(), mDepartmentInfo, mChooseDoctorInfo, mChooseScheduleInfo);
                }
            });
        }
        return mTipsDialog;
    }

    private void dismissBottomDialog() {
        if (mDoctorBottomDialog != null && mDoctorBottomDialog.isShowing()) {
            mDoctorBottomDialog.dismiss();
        }
    }

    private void dismissTipsDialog() {
        if (mTipsDialog != null && mTipsDialog.isShowing()) {
            mTipsDialog.dismiss();
        }
    }


    @Override
    public void onResponse(Request request, Response response) {
        if (!isAdded()) {
            return;
        }
        mRefreshView.onRefreshComplete();
        if (response.isResponseFail()) {
            ToastUtil.show(getActivity(), response.error);
        } else {
            if (response.targetData instanceof DoctorResponse) {
                DoctorResponse doctorResponse = response.cast(DoctorResponse.class);

                if (request.requestCode == HTTP_CODE_REFRESH) {
                    mDataSource.clear();
                }

                if (doctorResponse.page != null) {
                    mRefreshView.setCanLoadMore(!doctorResponse.page.lastPage);
                }

                mDataSource.addAll(doctorResponse.reginfos);
                mAdapter.setDataSource(mDataSource);
            } else if (response.targetData instanceof DoctorScheduleResponse) {
                mChooseDoctorInfo = (DoctorResponse.DoctorInfo) request.tag;

                DoctorScheduleResponse scheduleResponse = response.cast(DoctorScheduleResponse.class);
                ListBottomSheetDialog dialog = getChooseDialog(scheduleResponse.reginfos);
                TextView titleTv = dialog.getContentView().findViewById(R.id.tv_name);
                titleTv.setText(mChooseDoctorInfo.doctorname + "(" + mChooseDoctorInfo.doctorlevel + ")");
                dialog.show();
            }
        }
    }

    @Override
    public void onRefresh() {
        if (!isAdded()) {
            return;
        }
        if (mDateInfo != null && mDepartmentInfo != null) {
            mPageNo = 1;

            mDoctorApi.addRequestCode(HTTP_CODE_REFRESH);
            mDoctorApi.getDoctor(mSyscommunityCode, mDepartmentInfo.deptid, mDateInfo.startDate, mDateInfo.endDate, mPageNo);
        }
    }

    @Override
    public void onLastItemVisible() {
        if (!isAdded()) {
            return;
        }
        if (mDateInfo != null && mDepartmentInfo != null) {
            mPageNo += 1;

            mDoctorApi.addRequestCode(HTTP_CODE_LOAD_MORE);
            mDoctorApi.getDoctor(mSyscommunityCode, mDepartmentInfo.deptid, mDateInfo.startDate, mDateInfo.endDate, mPageNo);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        dismissBottomDialog();
        dismissTipsDialog();
    }
}
